export default {
    // 开启命名空间
    namespaced:true,
     // 静态数据存放处
     state:()=>({
        list:[
            { id: 100, name: "吃饭", isDone: true },
            { id: 201, name: "睡觉", isDone: false },
            { id: 103, name: "打豆豆", isDone: true },
        ],
        // 任务输入
        newtask:'',
        // 筛选完成未完成，默认是全部
        isOk:"all",
        selectAll:false
     }),
    mutations:{
        // 添加任务
        addtask(state,payload){
            if(payload=='') return alert('任务不能为空！')
            let id=state.list.length<=0 ?100 : state.list[state.list.length-1].id+1;
            state.list.push({
                id:id,
                name:payload,
                isDone:false
            });
            state.newtask=''
        },
        // 修改任务输入栏状态
        settask(state,payload){
            state.newtask=payload
        },
        // 根据id删除数据
        delId(state,payload){
            let index = state.list.findIndex(item=>item.id==payload)
            state.list.splice(index,1)
        },
        // 将state中的状态改变
        setIsOk(state,payload){
            state.isOk=payload
        },
        // 清除已完成
        clearIsOk(state){
            state.list = state.list.filter(item=>!item.isDone)
        },
        // 设置全选状态
        setAll(state,payload){
            state.selectAll=payload
            if(state.selectAll==false){
                state.list.forEach(item=>item.isDone=false)
            }else{
                state.list.forEach(item=>item.isDone=true)
            }
        }
    },
    getters:{
        // 点击未完成 查看未完成的所有任务
        showList(state){
            if(state.isOk=='no'){
                return state.list.filter(item=>!item.isDone)
            }else if(state.isOk=='yes'){
                return state.list.filter(item=>item.isDone)
            }
             return state.list
            },
            // 监听全选和反选
            isAll(state){
                state.selectAll= state.list.every(item=>item.isDone)
            }
        }
}
   